<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/images/logo.png" alt="">
        <span>宿舍管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 左侧导航栏-->
      <el-aside :width="isCollapsed ? '64px' : '200px' ">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu background-color="#333744" text-color="#fff"
          active-text-color="#409BFF" unique-opened :collapse="isCollapsed"
          :collapse-transition="false" router :default-active="activePath">
          <!-- 一级菜单 -->
          <el-submenu :index="item.menuNumber + '' " v-for="item in menuList" :key="item.menuNumber">
            <!-- 一级菜单的模板区 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsObj[item.menuNumber]"></i>
              <!-- 文本 -->
              <span>{{item.menuName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="subItem.twoMenuPath " v-for="subItem in item.levelTwoMenuList"
              @click="saveNavState(subItem.twoMenuPath)">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{subItem.twoMenuName}}</span>
              </template></el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
    export default {
      data() {
        return {
          //左侧菜单数据
          menuList: [],
          iconsObj: {
            '101': 'iconfont icon-wo',
            '103': 'iconfont icon-xuexiao',
            '105': 'iconfont icon-zhuzhaiqu',
            '107': 'iconfont icon-guanliyuan',
            '109': 'iconfont icon-banjixinxi',
            '114': 'iconfont icon-guanjiafuwu',
            '117': 'iconfont icon-anquanguanli',

            // '200': 'iconfont icon-xuexiao',
            // '202': 'iconfont icon-tubiaozhengli810-03',
            // '204': 'iconfont icon-guanjiafuwu'
          },
          //是否折叠
          isCollapsed: false,
          //被激活的链接地址
          activePath: '',

        }
      },
      //生命周期函数：初始化
      created(){
        this.getMenuList(); //初始化左侧导航列表
        this.activePath = window.sessionStorage.getItem("activePath");
      },
      methods: {

        /*获取左侧所有的菜单*/
        async getMenuList(){
          //从cookie中获取当前登录用户信息
          const userType = this.$cookies.get('user').userType;
          const {data : res} = await this.$axios.get(this.$http + '/otherManageController/getMenuList',{
            params: {
              userType: userType
            }
          });
          this.menuList = res.datas.menuList;
        },

        /*点击按钮，切换菜单的折叠与展开*/
        toggleCollapse() {
          this.isCollapsed = !this.isCollapsed;
        },

        /* 保存链接的激活状态*/
        saveNavState(activePath) {
          window.sessionStorage.setItem("activePath",activePath);
          this.activePath = activePath;
        },

        /*退出登录*/
        logout() {
          //清除sessionStorage
          window.sessionStorage.clear();
          //重定向到登录页
          this.$router.push('/login');
        }
      }
    }
</script>

<style lang="less" scoped>
  .home-container {
    height: 100%;
  }

  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 12px;
    align-items: center;
    color: #fff;
    font-size: 20px;
    > div {
      display: flex;
      align-items: center;
      span {
        margin-left: 15px;
      }
    }
    img{
      width: 50px;
      height: 50px;
      border-radius: 50%;
    }
  }

  .el-aside {
    background-color: #333744;
    .el-menu{
      border-right: none;
    }
  }

  .el-main {
    background-color: #eaedf1;
  }

  .iconfont{
    margin-right: 10px;
  }

  .toggle-button{
    background-color: #4A5064;
    font-size: 14px;
    line-height: 24px;
    color: #fff;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
</style>
